/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biojavagoalz.worker;

import biojavagoalz.string.StringOverlapFinder;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class that does the actual work
 * @author Tadas
 */
public class StringWorker extends Thread {

    private WorkManager workerManager;
    private StringOverlapFinder finder;

    public StringWorker(WorkManager workerManager, StringOverlapFinder finder) {
        this.workerManager = workerManager;
        this.finder = finder;
    }

    public WorkManager getWorkerManager() {
        return this.workerManager;
    }

    @Override
    public void run() {
        Item item;
        while ((item = getWorkerManager().getItem()) != null) {
            String[] st = item.getString().split(" ");
            String rez;
            if(st.length != 2){
                rez = "!! Non valid input";
            }
            else rez = finder.cleanX(st[0],
                    st[1]);
            while (getWorkerManager().getIndexCount() != item.getIndex()) {
                try {
                    sleep(10);
                } catch (InterruptedException ex) {
                    Logger.getLogger(StringWorker.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            //System.out.println(Thread.currentThread().getName()+ " rez: "+ rez);
            getWorkerManager().putResult(rez);
        }
        getWorkerManager().finishedWork();
        //System.out.println(Thread.currentThread().getName()+ " worker finished!");
    }
}
